package com.example.langchain4jdemo.modelService;


import com.example.langchain4jdemo.modelConfig.ChatModeConfig;
import dev.langchain4j.data.image.Image;
import dev.langchain4j.data.message.*;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.response.ChatResponse;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class ModelService {
    @Resource
    private
    ChatModeConfig chatModelConfig;

    /**
     * 有记忆对话
     * @param message
     * @return
     */
    public String chatWithMemory(String message) {
        String chatResponse = chatModelConfig.assistant.chat(message);
        log.info("AI 输出：" + chatResponse);
        return chatResponse;
    }

    /**
     * 无记忆对话
     * @param prompt
     * @return
     */
    public String chatNoMemory(String prompt) {
        UserMessage userMessage = UserMessage.from(prompt);
        ChatResponse chatResponse = chatModelConfig.qwenModel.chat(userMessage );
        AiMessage aiMessage = chatResponse.aiMessage();
        log.info("AI 输出：" + aiMessage.toString());
        return aiMessage.text();
    }

    /**
     * 图像识别
     * @param file
     * @param prompt
     * @return
     */
    public String recognition(Image file,String prompt){
        UserMessage userMessage = UserMessage.from(
                TextContent.from(prompt),
                ImageContent.from(file)
        );
        ChatResponse response = chatModelConfig.qwenModel.chat(userMessage);
        return response.aiMessage().text();
    }
}
